package com.cuvretool.view.mousekeyboard.workspace;

import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseEvent;
import java.util.Timer;
import java.util.TimerTask;

import com.cuvretool.View;
import com.cuvretool.utils.Vector2;
import com.cuvretool.view.coordinate.RulerDrawInfo;
import com.cuvretool.view.draw.DisplayDraw;
import com.cuvretool.view.inputlistener.ViewInputHandleListener;
import com.cuvretool.view.mousekeyboard.CurveToolMouseEvent;
/**
 * 绘制区鼠标事件
 * since 2015年3月12日 下午2:25:00
 * @author boy
 */
public class MouseEventWorkSpace extends CurveToolMouseEvent {
	// ===========================================================
	// Constants
	// ===========================================================

	// ===========================================================
	// Fields
	// ===========================================================
	/**外抛接口*/
	private ViewInputHandleListener mViewInputHandleListener;
	/**标尺绘制需求参数结构定义*/
	private RulerDrawInfo mRulerDrawInfo;
	
	/**点击事件计数*/
	private int mClickNumber = 0;
	/**点击的屏幕坐标记录*/
	private Vector2 mClickPosition;
	// ===========================================================
	// Constructors
	// ===========================================================
	public MouseEventWorkSpace(){
		mViewInputHandleListener = View.getViewInputHandleListener();
		mClickPosition = new Vector2();
	}
	// ===========================================================
	// Getter & Setter
	// ===========================================================
	/**
	 * 设置网格标尺
	 * @param rulerDrawInfo
	 */
	public void setRulerDrawInfo(RulerDrawInfo rulerDrawInfo){
		mRulerDrawInfo = rulerDrawInfo;
	}
	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================
	@Override
	public void mouseClicked(MouseEvent e) {
		/**
		 * 1、点击删除某点
		 * 2、双击添加某点
		 */
		mClickPosition.set(e.getX(), e.getY());
		if(e.getButton() == MouseEvent.BUTTON1){
			mClickNumber ++;
			Timer timer = new Timer();
			timer.schedule(new TimerTask() {
				@Override
				public void run() {
					boolean flush = mViewInputHandleListener.onClickEventWorkSpace(mClickPosition, mClickNumber);
					mBaseJPanel.requestFocus();
					mClickNumber = 0;
					mClickPosition.set(0, 0);
					if(flush){
						mBaseJPanel.repaint();
					}
				}
			}, 300);
		}
	}

	@Override
	public void mousePressed(MouseEvent e) {
		/**
		 * 1、按住某点，移动点前的标志
		 * 2、按住绘制区，移动绘制区前的标志
		 */
		if(e.getButton() == MouseEvent.BUTTON1){
			mPressedLeft = new Vector2(e.getX(), e.getY());
			mMoveLeft = new Vector2(mPressedLeft);
			isPressedLeft = true;
			mViewInputHandleListener.onPressed(MouseEvent.BUTTON1,mPressedLeft);
		} else if (e.getButton() == MouseEvent.BUTTON3) {
			mPressedRight = new Vector2(e.getX(), e.getY());
			mMoveRight = new Vector2(mPressedRight);
			isPressedRight = true;
			mViewInputHandleListener.onPressed(MouseEvent.BUTTON3,mPressedRight);
		}
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		/**
		 * 1、移动某点，左键生效
		 * 2、移动绘制区，右键生效
		 */
		int x,y;
		boolean isRequireRepaint = false;
		if(isPressedLeft){
			x = e.getX();
			y = e.getY();
			isRequireRepaint = mViewInputHandleListener.onMovePoint(new Vector2(x, y));
		} else if (isPressedRight) {
			x = e.getX() - (int)mMoveRight.x;
			y = e.getY() - (int)mMoveRight.y;
			mMoveRight.x = e.getX();
			mMoveRight.y = e.getY();
			DisplayDraw displayDraw = DisplayDraw.obtain();
			mRulerDrawInfo.mBeginX += displayDraw.TransformDisplayToDrawLenX(-x);
			mRulerDrawInfo.mBeginY += displayDraw.TransformDisplayToDrawLenY( y);
			mRulerDrawInfo.run();
			DisplayDraw.obtain().run();
			isRequireRepaint = true;
		}
		if(isRequireRepaint){
			mBaseJPanel.repaint();
		}
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		/**
		 * 鼠标释放,执行清理
		 */
		if(e.getButton() == MouseEvent.BUTTON1){
			mPressedLeft = null;
			mMoveLeft = null;
			isPressedLeft = false;
			mViewInputHandleListener.onReleased(MouseEvent.BUTTON1);
		} else if (e.getButton() == MouseEvent.BUTTON3) {
			mPressedRight = null;
			mMoveRight = null;
			isPressedRight = false;
			mViewInputHandleListener.onReleased(MouseEvent.BUTTON3);
		}
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		/**
		 * 1、鼠标停留某点，显示该点属性
		 */
		boolean is = false;
		is = mViewInputHandleListener.onMove(new Vector2(e.getX(), e.getY()));
		if(is){
			mBaseJPanel.repaint();
		}
	}

	@Override
	public void mouseWheelMoved(MouseWheelEvent e) {
		/**
		 * 1、缩放绘制区绘制比例大小
		 */
		int rotation = e.getWheelRotation();
		mRulerDrawInfo.mSpacingX += rotation;
		mRulerDrawInfo.mSpacingY += rotation;
		if(mRulerDrawInfo.mSpacingX < 1){
			mRulerDrawInfo.mSpacingX = 1;
		}
		if(mRulerDrawInfo.mSpacingY < 1){
			mRulerDrawInfo.mSpacingY = 1;
		}
		mRulerDrawInfo.run();
		DisplayDraw.obtain().run();
		mBaseJPanel.repaint();
	}
	// ===========================================================
	// Methods
	// ===========================================================

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
